package bb.lanxing

import android.app.Application
import android.content.Context
import android.net.Uri
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import bb.lanxing.device.thirdpart.gpx.GPXImportViewModel
import bb.lanxing.model.database.RouteBook
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith


/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
class KotlinAndroidTest {
    private lateinit var appContext: Context
    private lateinit var mViewModel: GPXImportViewModel

    companion object {
        fun test() {
            println("static test")
        }
    }

    @Before
    fun setUp() {
        println("setUp")
        appContext = InstrumentationRegistry.getInstrumentation().targetContext
        mViewModel = ViewModelProvider(
            ViewModelStore(),
            ViewModelProvider.AndroidViewModelFactory(appContext.applicationContext as Application)
        )[GPXImportViewModel::class.java]
    }

    @After
    fun after() {
        println("after")
    }


    @Test
    fun testDb() {
        val userId = App.getInstance().userId
        println("testDb userId = $userId")
        val list = RouteBook.getList()
        list.forEach {
            println("list it = $it")
        }
        val listFilter = list.filter { routeBook -> userId == routeBook.userId }.toList()
        listFilter.forEach {
            println("listFilter it = $it")
        }
    }

    @Test
    fun useAppContext() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getInstrumentation().targetContext
        Assert.assertEquals("bb.lanxing", appContext.packageName)
    }

    @Test
    fun gpxTest() {
        val uriStr = "content://com.android.externalstorage.documents/document/primary%3A26770c65-4fb5-4061-8e7f-552d39d9f9bd.gpx"
        val uri = Uri.parse(uriStr)
        mViewModel.generateGpx(appContext, uri)
    }
}